package com.niit.ai.dl.dltrain.web;

import com.niit.ai.dl.dltrain.service.FileService;
import com.niit.ai.dl.dltrain.web.model.FileUploadRsp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
public class FileController {
    private static final Logger LOG = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService fileService;

    @RequestMapping(value = "/uploadSample", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public FileUploadRsp uploadSample(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        LOG.info("req name = {}, OriginalFilename = {}", file.getName(), file.getOriginalFilename());
        FileUploadRsp rsp = fileService.uploadSample(file, request);
        LOG.info("rsp {}", rsp);
        return rsp;
    }

    @RequestMapping(value = "/uploadModel", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public FileUploadRsp uploadModel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        LOG.info("req name = {}, OriginalFilename = {}", file.getName(), file.getOriginalFilename());
        FileUploadRsp rsp = fileService.uploadModel(file, request);
        LOG.info("rsp {}", rsp);
        return rsp;
    }


    @RequestMapping(value = "/downLoad", method = RequestMethod.GET)
    public String downLoadFile(@RequestParam("fileName") String filePath, HttpServletResponse response) throws Exception {
        LOG.info("req {}", filePath);
        String result = fileService.downLoad(filePath, response);
        LOG.info("rsp");
        return result;
    }
}
